<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;

class Galleries extends Common
{
   protected $middleware = ['Auth'];

   /**
    * 显示资源列表
    *
    * @return \think\Response
    */
   public function index()
   {
      $kepians = Db::table('galleries')->where('category_id', 1)->order('id')->paginate(27);
      $yangpians = Db::table('galleries')->where('category_id', 2)->order('id')->paginate(27);

      return view('Galleries/index', compact('kepians', 'yangpians'));
   }

   /**
    * 显示创建资源表单页.
    *
    * @return \think\Response
    */
   public function create()
   {
      return view('Galleries/create');
   }

   /**
    * 保存新建的资源
    *
    * @param  \think\Request $request
    * @return \think\Response
    */
   public function save(Request $request)
   {
      if ($request->has('imgs')) {
         foreach ($request->param('imgs') as $img) {
            Db::table('galleries')->insert([
               'category_id' => $request->param('category_id'),
               'img' => $img,
               'create_at' => date('Y-m-d H:i:s')
            ]);
         }
      }
      return redirect('Galleries/index')->with('success', '上传图片成功~');
   }

   /**
    * 显示指定的资源
    *
    * @param  int $id
    * @return \think\Response
    */
   public function read($id)
   {
      //
   }

   /**
    * 显示编辑资源表单页.
    *
    * @param  int $id
    * @return \think\Response
    */
   public function edit($id)
   {

   }

   /**
    * 保存更新的资源
    *
    * @param  \think\Request $request
    * @param  int $id
    * @return \think\Response
    */
   public function update(Request $request, $id)
   {
//      $id = $request->param('id');
      $title = $request->param('title');
//      Db::table('galleries')->where('id', $id)->setField('title', $title);
      Db::table('galleries')->update(['title' => $title, 'id' => $id]);
   }

   /**
    * 删除指定资源
    *
    * @param  int $id
    * @return \think\Response
    */
   public function delete($id)
   {

   }

   /***
    * 多选删除
    */
   public function delete_all(Request $request)
   {
      Db::table('galleries')->whereIn('id', $request->check_id)->delete();
   }


}
